/***     
 * 查找顺子
 */
var _ = require('./../../FrameWork/we_underscore');
var FindCardTool = require('./FindCardTool');
var FindShunZiTool = module.exports = {};

/***查找顺子
 * _cardInfoList:[],
 * _shunZiNumberMin:1,
 * _shunZiNumberMax:10
 * return 2表示顺的数量2
 */
FindShunZiTool.isShunZiSection = function(_cardInfoList, _shunZiNumberMin, _shunZiNumberMax) {
    var value = 0;
    for (var i = _shunZiNumberMax; i >= _shunZiNumberMin; --i) {
        var isFind = this.isShunZi(_cardInfoList, i);
        if (isFind) {
            value = i;
            break;
        }
    }
    return value;
};

/**是否为顺子 
 * _cardInfoList：[]
 * _shunZiCnt:xx//顺子的个数默认5个
 */
FindShunZiTool.isShunZi = function(_cardInfoList, _shunZiCnt) {
    _shunZiCnt = null == _shunZiCnt ? 5 : _shunZiCnt;
    var size = _.size(_cardInfoList);
    if (size < _shunZiCnt) {
        return false;
    }
    _cardInfoList = _.sortBy(_cardInfoList, function(_cardInfo) {
        return _cardInfo.value;
    });

    for (var i = 0; i <= (size - _shunZiCnt); ++i) {
        var fristCardInfo = _cardInfoList[i];
        var fristValue = fristCardInfo.value;
        var isFind = true;
        for (var j = (i + 1); j < size; ++j) {
            var tmpValue = _cardInfoList[j].value;
            if ((fristValue + 1) == tmpValue) {
                fristValue = tmpValue;
            } else {
                isFind = false;
                break;
            }
        }
        if (isFind) {
            return true;
        }
    }

    return false;
    // var tmpCardVale = _cardInfoList[0].value;
    // for (var i = 1; i < size; ++i) {
    //     if ((tmpCardVale + 1) == _cardInfoList[i].value) {
    //         tmpCardVale += 1;
    //     } else {
    //         isFind = false;
    //         break;
    //     }
    // }
    return isFind;
};

/*区间找顺子(只找数量最多的那种) */
FindShunZiTool.getResultSection = function(_cardInfoList, shunZiNumberMin, shunZiNumberMax) {
    var result = null
    for (var i = shunZiNumberMax; i >= shunZiNumberMin; --i) {
        result = this.getMoreResult(_cardInfoList, i);
        if (result.isFind) {
            return result;
        }
    }
    return result;
};

/*区间找顺子 */
FindShunZiTool.getMoreResultSection = function(_cardInfoList, shunZiNumberMin, shunZiNumberMax) {
    var resultMore = {
        isFind: false,
        findGroupMore: [],
        otherGroup: []
    };
    for (var i = shunZiNumberMax; i >= shunZiNumberMin; --i) {
        var result = this.getMoreResult(_cardInfoList, i);
        if (result.isFind) {
            _.each(result.findGroupMore, function(info) {
                resultMore.findGroupMore.push(info);
            })
        }
    }
    resultMore.isFind = _.size(resultMore.findGroupMore) > 0;
    return resultMore;
};

/***
 * shunZiNumber:顺子个数
 * return {
 *  isFind : false,
 *  findGroupMore:[{shunzi:[],otherGroup:[]}] 或者 _cardInfoList
 * }
 */
FindShunZiTool.getMoreResult = function(_cardInfoList, shunZiNumber) {
    var resultInfo = this.getResult(_cardInfoList, shunZiNumber);
    if (resultInfo.isFind) {
        var result = {
            isFind: true,
            findGroupMore: []
        }
        var tmpFindGroup = resultInfo.findGroup;
        _.each(tmpFindGroup, function(oneGroupCardInfo) {
            var otherGroup = _.difference(_cardInfoList, oneGroupCardInfo);
            result.findGroupMore.push({
                findGroup: oneGroupCardInfo,
                otherGroup: otherGroup
            });
        }.bind(this));

        return result;
    }
    return resultInfo;
};

/***
 * shunZiNumber:顺子个数
 */
FindShunZiTool.getResult = function(_cardInfoList, shunZiNumber) {
    var resultInfo = this.check(_cardInfoList, shunZiNumber);
    if (resultInfo.isFind) {
        return resultInfo;
    }
    return resultInfo;
};

FindShunZiTool.check = function(_cardInfoList, shunZiNumber) {
    _cardInfoList = _.sortBy(_cardInfoList, 'value');
    var size = _.size(_cardInfoList);

    var result = {
        isFind: false,
        findGroup: [],
        otherGroup: _cardInfoList
    };
    //数组数量都不够
    if (size < shunZiNumber) {
        // console.debug("牌数量未达到%d个不可能形成%d连的顺子", shunZiNumber, shunZiNumber);
        return result;
    }

    var fristCardInfo = _cardInfoList[0];
    var lastCardInfo = _cardInfoList[size - 1];

    var tmpValue = Math.abs(lastCardInfo.value - fristCardInfo.value);
    if (tmpValue < (shunZiNumber - 1)) {
        // console.debug('最大值减去最小值等于%d不可能形成%d连的顺子', tmpValue, shunZiNumber);
        return result;
    }
    //下面是可能存在的逻辑

    //可能形成的组数
    var canGroupNumber = (size - shunZiNumber) + 1;

    //可以组合的情况
    var canGroupList = [];
    var oldFindCardId = null;
    for (var i = 0; i < canGroupNumber; ++i) {
        var beginIdxList = []; //顺子的开始位置
        var findNextCard = _cardInfoList[i].value;

        /***避免重复计算 */
        if (null != oldFindCardId) {
            if (oldFindCardId == findNextCard) {
                continue;
            }
        }
        oldFindCardId = findNextCard;

        // console.debug('查找的第一张牌：', findNextCard);
        var _tmpFind = true;
        var findGroup = [_cardInfoList[i]];
        for (var j = 1; j < shunZiNumber; ++j) {
            findNextCard += 1;
            var _tmpResult = FindCardTool.getResult(_cardInfoList, function(_cardInfo) {
                return findNextCard == _cardInfo.value;
            }, function(_cardInfo) {
                return findNextCard != _cardInfo.value;
            });
            if (!_tmpResult.isFind) {
                _tmpFind = false;
                break;
            }
            findGroup.push(_tmpResult.findGroup[0]);
        }
        if (_tmpFind) {
            canGroupList.push(findGroup);
            // console.debug('找到%d到%d的顺子', (findNextCard - shunZiNumber + 1), findNextCard);
        }
    }
    return {
        isFind: _.size(canGroupList) > 0,
        findGroup: canGroupList, //
        otherGroup: _cardInfoList
    };
}